package io.xccit.race.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.xccit.race.mapper.RaceCompetitionResultMapper;
import io.xccit.race.domain.RaceCompetitionResult;
import io.xccit.race.service.IRaceCompetitionResultService;
import io.xccit.common.utils.SecurityUtils;

/**
 * 竞赛成果物Service业务层处理
 * 
 * @author ywx
 * @date 2025-10-13
 */
@Service
public class RaceCompetitionResultServiceImpl implements IRaceCompetitionResultService {
    
    @Autowired
    private RaceCompetitionResultMapper raceCompetitionResultMapper;

    /**
     * 查询竞赛成果物
     * 
     * @param id 竞赛成果物主键
     * @return 竞赛成果物
     */
    @Override
    public RaceCompetitionResult selectRaceCompetitionResultById(Long id) {
        return raceCompetitionResultMapper.selectRaceCompetitionResultById(id);
    }

    /**
     * 查询竞赛成果物列表
     * 
     * @param raceCompetitionResult 竞赛成果物
     * @return 竞赛成果物
     */
    @Override
    public List<RaceCompetitionResult> selectRaceCompetitionResultList(RaceCompetitionResult raceCompetitionResult) {
        return raceCompetitionResultMapper.selectRaceCompetitionResultList(raceCompetitionResult);
    }

    /**
     * 新增竞赛成果物
     * 
     * @param raceCompetitionResult 竞赛成果物
     * @return 结果
     */
    @Override
    public int insertRaceCompetitionResult(RaceCompetitionResult raceCompetitionResult) {
        if (raceCompetitionResult.getDelFlag() == null) {
            raceCompetitionResult.setDelFlag("0");
        }
        if (raceCompetitionResult.getAuditStatus() == null) {
            raceCompetitionResult.setAuditStatus(0); // 默认待审核
        }
        if (raceCompetitionResult.getSubmitTime() == null) {
            raceCompetitionResult.setSubmitTime(new Date());
        }
        // 确保设置用户ID
        if (raceCompetitionResult.getUserId() == null) {
            raceCompetitionResult.setUserId(SecurityUtils.getUserId());
        }
        // 确保设置用户名
        if (raceCompetitionResult.getUserName() == null) {
            raceCompetitionResult.setUserName(SecurityUtils.getUsername());
        }
        
        // 验证文件相关字段不能为空
        if (raceCompetitionResult.getFilePath() == null || raceCompetitionResult.getFilePath().trim().isEmpty()) {
            throw new RuntimeException("文件路径不能为空，请先上传文件");
        }
        if (raceCompetitionResult.getFileSize() == null || raceCompetitionResult.getFileSize() <= 0) {
            throw new RuntimeException("文件大小不能为空或为0");
        }
        if (raceCompetitionResult.getFileMd5() == null || raceCompetitionResult.getFileMd5().trim().isEmpty()) {
            throw new RuntimeException("文件MD5值不能为空");
        }
        
        return raceCompetitionResultMapper.insertRaceCompetitionResult(raceCompetitionResult);
    }

    /**
     * 修改竞赛成果物
     * 
     * @param raceCompetitionResult 竞赛成果物
     * @return 结果
     */
    @Override
    public int updateRaceCompetitionResult(RaceCompetitionResult raceCompetitionResult) {
        return raceCompetitionResultMapper.updateRaceCompetitionResult(raceCompetitionResult);
    }

    /**
     * 批量删除竞赛成果物
     * 
     * @param ids 需要删除的竞赛成果物主键
     * @return 结果
     */
    @Override
    public int deleteRaceCompetitionResultByIds(Long[] ids) {
        return raceCompetitionResultMapper.deleteRaceCompetitionResultByIds(ids);
    }

    /**
     * 删除竞赛成果物信息
     * 
     * @param id 竞赛成果物主键
     * @return 结果
     */
    @Override
    public int deleteRaceCompetitionResultById(Long id) {
        return raceCompetitionResultMapper.deleteRaceCompetitionResultById(id);
    }

    /**
     * 根据竞赛ID查询成果物列表
     * 
     * @param competitionId 竞赛ID
     * @return 成果物列表
     */
    @Override
    public List<RaceCompetitionResult> selectRaceCompetitionResultByCompetitionId(Long competitionId) {
        return raceCompetitionResultMapper.selectRaceCompetitionResultByCompetitionId(competitionId);
    }

    /**
     * 根据报名记录ID查询成果物
     * 
     * @param signupId 报名记录ID
     * @return 成果物
     */
    @Override
    public RaceCompetitionResult selectRaceCompetitionResultBySignupId(Long signupId) {
        return raceCompetitionResultMapper.selectRaceCompetitionResultBySignupId(signupId);
    }

    /**
     * 根据用户ID查询成果物列表
     * 
     * @param userId 用户ID
     * @return 成果物列表
     */
    @Override
    public List<RaceCompetitionResult> selectRaceCompetitionResultByUserId(Long userId) {
        return raceCompetitionResultMapper.selectRaceCompetitionResultByUserId(userId);
    }

    /**
     * 统计成果物数量
     * 
     * @return 成果物总数
     */
    @Override
    public Long selectRaceCompetitionResultCount() {
        return raceCompetitionResultMapper.selectRaceCompetitionResultCount();
    }

    /**
     * 统计审核状态分布
     * 
     * @return 审核状态统计
     */
    @Override
    public List<Map<String, Object>> selectAuditStatusDistribution() {
        return raceCompetitionResultMapper.selectAuditStatusDistribution();
    }

    /**
     * 统计竞赛成果物数量
     * 
     * @param competitionId 竞赛ID
     * @return 成果物数量
     */
    @Override
    public Long selectResultCountByCompetitionId(Long competitionId) {
        return raceCompetitionResultMapper.selectResultCountByCompetitionId(competitionId);
    }

    /**
     * 审核成果物
     * 
     * @param raceCompetitionResult 成果物信息
     * @return 结果
     */
    @Override
    public int auditRaceCompetitionResult(RaceCompetitionResult raceCompetitionResult) {
        // 设置审核人信息
        raceCompetitionResult.setAuditorId(SecurityUtils.getUserId());
        raceCompetitionResult.setAuditorName(SecurityUtils.getUsername());
        raceCompetitionResult.setAuditTime(new Date());
        
        return raceCompetitionResultMapper.updateRaceCompetitionResult(raceCompetitionResult);
    }

    /**
     * 提交成果物
     * 
     * @param raceCompetitionResult 成果物信息
     * @return 结果
     */
    @Override
    public int submitRaceCompetitionResult(RaceCompetitionResult raceCompetitionResult) {
        // 设置提交人信息
        raceCompetitionResult.setUserId(SecurityUtils.getUserId());
        raceCompetitionResult.setUserName(SecurityUtils.getUsername());
        raceCompetitionResult.setSubmitTime(new Date());
        raceCompetitionResult.setAuditStatus(0); // 待审核
        
        return insertRaceCompetitionResult(raceCompetitionResult);
    }
}
